package com.wang.studentmanager;

import java.util.ArrayList;
import java.util.Scanner;



public class Method {
    public static void start(){
        System.out.println("-----------------");
        System.out.println("1、显示全部学员");
        System.out.println("2、增加学员");
        System.out.println("3、删除学员");
        System.out.println("4、修改学员信息");
        System.out.println("0、退出");
        System.out.println("-----------------");
    }
    public static int getIndex(ArrayList<Student> arrayList,Scanner sc,String stuNum){
        int index = -1;
        for (int i = 0; i < arrayList.size(); i++) {
            Student student = arrayList.get(i);
            if (student.getStuNum().equals(stuNum)){
                index = i;
                break;
            }
        }
        return index;
    }
    public static void add(ArrayList<Student> arrayList,Scanner sc){
        System.out.println("请输入新增学生学号：");
        String stuNum = sc.next();
        int index = getIndex(arrayList,sc,stuNum);
        if (index != -1){
            System.out.println("学号已存在，请重新输入！");
        } else {
            System.out.println("请输入新增学生年龄：");
            int age = sc.nextInt();
            System.out.println("请输入新增学生姓名：");
            String name = sc.next();
            System.out.println("请输入新增学生身份证号：");
            String IDNum = sc.next();
            arrayList.add(new Student(stuNum,age,name,IDNum));
            System.out.println("新增学生信息成功！");
        }
    }

    public static void showInfo(ArrayList<Student> arrayList){
        System.out.println("学号\t\t\t年龄\t\t姓名\t\t\t身份证号");
        for (int i = 0; i < arrayList.size(); i++) {
            Student student = arrayList.get(i);
            System.out.println(student.getStuNum() + "\t" + student.getAge() + "\t\t" + student.getName() + "\t\t" + student.getIDNum());
        }
    }

    public static void delete(ArrayList<Student> arrayList,Scanner sc){
        System.out.println("请输入要删除的学生的学号：");
        String stuNum = sc.next();
        int index = getIndex(arrayList,sc,stuNum);
        if (index == -1){
            System.out.println("您输入的学号不存在，请重新输入！");
        } else {
            arrayList.remove(index);
            System.out.println("学生信息删除成功!");
        }
    }

    public static void update(ArrayList<Student> arrayList,Scanner sc){
        System.out.println("请输入要修改的学生的学号：");
        String stuNum = sc.next();
        int index = getIndex(arrayList,sc,stuNum);
        if (index == -1){
            System.out.println("您输入的学号不存在，请重新输入！");
        } else {
            System.out.println("请输入要修改的学生年龄：");
            int age = sc.nextInt();
            System.out.println("请输入要修改的学生姓名：");
            String name = sc.next();
            System.out.println("请输入要修改的身份证号：");
            String IDNum = sc.next();
            arrayList.set(index,new Student(stuNum,age,name,IDNum));
            System.out.println("学生信息修改成功!");
        }
    }
}
